package com.dayu.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dayu.constant.Constants;
import com.dayu.entity.Merchant;
import com.dayu.service.MerchantService;

/**
 * @author dayu
 * @date 2018/11/01
 */
@Controller
@RequestMapping("merchant")
public class MerchantController {

    @Autowired
    private MerchantService merchantService;

    @RequestMapping("merchantList")
    public String merchantList(Model model) {
        List<Merchant> merchantList = merchantService.getMerchantListByType(Constants.MerchantType.All);
        model.addAttribute("merchantList", merchantList);
        return "merchant/merchantList";
    }

    @RequestMapping("gotoAdd")
    public String gotoAdd() {
        return "merchant/gotoAdd";
    }

    @RequestMapping("add")
    public String add(Merchant merchant, Model model) {
        System.out.println("MerchantController.add()");
        System.out.println(merchant);
        merchantService.insertMerchant(merchant);
        List<Merchant> merchantList = merchantService.getMerchantListByType(Constants.MerchantType.All);
        model.addAttribute("merchantList", merchantList);
        return "merchant/merchantList";
    }

    @RequestMapping("gotoUpdateMerchant")
    public String gotoUpdateMerchant(int merchantId, Model model) {
        Merchant merchant = merchantService.getMerchantById(merchantId);
        model.addAttribute("merchant", merchant);
        return "merchant/updateMerchant";
    }

    @RequestMapping("deleteMerchant")
    public String deleteMerchant(int merchantId, Model model) {
        merchantService.deleteMerchantById(merchantId);
        return "redirect:merchantList";
    }

    @RequestMapping("updateMerchant")
    public String updateMerchant(Merchant merchant, Model model) {
        merchantService.updateMerchant(merchant);
        return "redirect:merchantList";
    }

}
